package net.anzhuo.controller.db.report;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsBranch;
import net.anzhuo.entity.GsChange;
import net.anzhuo.entity.GsGonggao;
import net.anzhuo.entity.GsGudong;
import net.anzhuo.entity.GsMember;
import net.anzhuo.entity.GsOuttouzi;
import net.anzhuo.entity.GsReportLog;
import net.anzhuo.entity.GsShangbiao;
import net.anzhuo.entity.GsYichang;
import net.anzhuo.entity.GsZhuanli;
import net.anzhuo.entity.GsZhuzuoquan;
import net.anzhuo.entity.db.UploadSecondData;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsBranchService;
import net.anzhuo.service.GsChangeService;
import net.anzhuo.service.GsGonggaoService;
import net.anzhuo.service.GsGudongService;
import net.anzhuo.service.GsMemberService;
import net.anzhuo.service.GsOuttouziService;
import net.anzhuo.service.GsReportLogService;
import net.anzhuo.service.GsShangbiaoService;
import net.anzhuo.service.GsYichangService;
import net.anzhuo.service.GsZhuanliService;
import net.anzhuo.service.GsZhuzuoquanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * 征信报告接口
 */
@RestController
@Slf4j
@RequestMapping("/creditReport/")
public class ReportController {

    @Autowired
    private GsBaseService gsBaseService;
    @Autowired
    private GsChangeService gsChangeService;
    @Autowired
    private GsBranchService gsBranchService;
    @Autowired
    private GsGudongService gsGudongService;
    @Autowired
    private GsReportLogService gsReportLogService;
    @Autowired
    private GsOuttouziService gsOuttouziService;
    @Autowired
    private GsZhuzuoquanService gsZhuzuoquanService;
    @Autowired
    private GsZhuanliService gsZhuanliService;
    @Autowired
    private GsShangbiaoService gsShangbiaoService;
    @Autowired
    private GsYichangService gsYichangService;
    @Autowired
    private  GsGonggaoService gsGonggaoService;
    @Autowired
    private GsMemberService gsMemberService;
    private  Integer updateIndex = 0;
    private Integer insertIndex  = 0 ;


    private String dealBigDecimal(BigDecimal value) {
        DecimalFormat df2 = new DecimalFormat("###0.00");
        if (value == null) {
            return "—";
        } else {
            return df2.format(value);
        }
    }



    @RequestMapping("insert")
    @ResponseBody
    public boolean insert(@RequestBody UploadSecondData uploadSecondData) throws Exception {

        Map<Object, String> returnMap = new HashMap<>();

        System.out.println("--------------" + JSON.toJSONString(uploadSecondData));
        Long time = System.currentTimeMillis();
        /**基本信息表*/
        GsBase gsBase = new GsBase();
        GsBase companyName = gsBaseService.getByName(uploadSecondData.getComponyName());
        if(StringUtils.isEmpty(uploadSecondData.getCreditCode()))
        {
        	return true;
        }
        GsBase creditCode = gsBaseService.getByCreditCode(uploadSecondData.getCreditCode());
        log.debug("查询用了" + String.valueOf(System.currentTimeMillis()-time/1000));

        if (companyName != null || creditCode != null) {

            updateIndex ++ ;

            if (companyName != null) {
                gsBase = companyName;
            }
            if (creditCode != null) {
                gsBase = creditCode;
            }
            try {
                gsBase.setName(uploadSecondData.getComponyName());
                gsBase.setCreditCode(uploadSecondData.getCreditCode());
//                gsBase.setLegal(uploadLocalData.getComponyBasicInfo().getFr());
//                gsBase.setRegAddress(uploadLocalData.getComponyBasicInfo().getAdress());
//                gsBase.setEnterpriseStatus(uploadLocalData.getComponyBasicInfo().getEconomicType());
//                gsBase.setRegZb(uploadLocalData.getComponyBasicInfo().getRegistMoney());
//                gsBase.setTel(uploadLocalData.getComponyBasicInfo().getContactTel());
//                gsBase.setOperScope(uploadLocalData.getComponyBasicInfo().getScope());
//                gsBase.setYear(String.valueOf(uploadLocalData.getComponyBasicInfo().getEstablishment()));
//                gsBase.setRegTime(String.valueOf(uploadLocalData.getComponyBasicInfo().getCreateTime()));
                gsBase.setEnterpriseType(uploadSecondData.getEconKind());//公司类型
                gsBase.setEmail(uploadSecondData.getEmail());
                gsBase.setIndustry(uploadSecondData.getIndustry());//行业
                gsBaseService.update(gsBase);
            } catch (Exception e) {
                log.error("",e);
                log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                return false;
            }
        } else if (companyName == null && creditCode == null) {
            try {
                insertIndex ++ ;
                gsBase.setName(uploadSecondData.getComponyName());
                gsBase.setCreditCode(uploadSecondData.getCreditCode());
//                gsBase.setLegal(uploadLocalData.getComponyBasicInfo().getFr());
//                gsBase.setRegAddress(uploadLocalData.getComponyBasicInfo().getAdress());
//                gsBase.setEnterpriseStatus(uploadLocalData.getComponyBasicInfo().getEconomicType());
//                gsBase.setRegZb(uploadLocalData.getComponyBasicInfo().getRegistMoney());
//                gsBase.setTel(uploadLocalData.getComponyBasicInfo().getContactTel());
//                gsBase.setOperScope(uploadLocalData.getComponyBasicInfo().getScope());
//                gsBase.setYear(String.valueOf(uploadLocalData.getComponyBasicInfo().getEstablishment()));
//                gsBase.setRegTime(String.valueOf(uploadLocalData.getComponyBasicInfo().getCreateTime()));
                gsBase.setMd5(UUID.randomUUID().toString().replace("-", ""));
                gsBase.setEnterpriseType(uploadSecondData.getEconKind());//公司类型
                gsBase.setEmail(uploadSecondData.getEmail());
                gsBase.setIndustry(uploadSecondData.getIndustry());//行业
                log.debug("md5----------:" + gsBase.getMd5());
                gsBaseService.save(gsBase);
            } catch (Exception e) {
                log.error("",e);
                log.debug("insert异常原因"+ JSON.toJSON(uploadSecondData));
                return false;
            }
            System.out.println("insert-----------------------------------------" +insertIndex);
            System.out.println("upadte-----------------------------------------" +updateIndex);
        }


        /**变更信息表*/
   /*     GsChange gsChange = new GsChange();
        List<GsChange> md5Change = gsChangeService.getByMd5(gsBase.getMd5());
        List<UploadSecondData.ChangeRecordsBean> listChange = uploadSecondData.getChangeRecords();
        for (int i = 0; i < md5Change.size(); i++) {
            log.debug("开始更新change表："+JSON.toJSON(uploadSecondData));
                gsChange.setCoMd5(gsBase.getMd5());
                gsChangeService.delete(gsChange);
                for (int j = 0; j < listChange.size(); j++) {
                    try {
//                        gsChange.setChangeBefore(listChange.get(j).getBeforeRecord());
//                        gsChange.setChangeAfter(listChange.get(j).getAfterRecord());
//                        gsChange.setChangeTime(String.valueOf(listChange.get(j).getDateApproval()));
                        gsChange.setChangeProject(listChange.get(j).getProjectName());
//                        gsChange.setId(Long.valueOf(md5Change.get(i).getId()));
                        gsChangeService.save(gsChange);
                    } catch (Exception e) {
                        log.error("",e);
                        log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
        if(listChange!=null){
        for (int j = 0; j < listChange.size(); j++) {
            if (md5Change.size() == 0 && (listChange.get(j).getProjectName() != null)) {
                log.debug("开始insert change表："+JSON.toJSON(uploadSecondData));
                try {
//                    gsChange.setChangeBefore(listChange.get(j).getBeforeRecord());
//                    gsChange.setChangeAfter(listChange.get(j).getAfterRecord());
//                    gsChange.setChangeTime(String.valueOf(listChange.get(j).getDateApproval()));
                    gsChange.setCoMd5(gsBase.getMd5());
//                    gsChange.setMd5(UUID.randomUUID().toString().replace("-",""));
                    gsChange.setChangeProject(listChange.get(j).getProjectName());
                    gsChangeService.save(gsChange);
                } catch (Exception e) {
                    log.error("",e);
                    log.debug("insert异常原因"+ JSON.toJSON(uploadSecondData));
                    return false;
                }

            }
        }
        }*/

        /**分支机构表*/
        GsBranch gsBranch = new GsBranch();
        List<GsBranch> md5Branch = gsBranchService.getByMd5(gsBase.getMd5());
        List<UploadSecondData.BranchesesBean> branchesList = uploadSecondData.getBrancheses();
        for (int i = 0; i < md5Branch.size(); i++) {
            log.debug("开始更新branch表："+JSON.toJSON(uploadSecondData));
                gsBranch.setCoMd5(gsBase.getMd5());
                gsBranchService.delete(gsBranch);
                for (int j = 0; j < branchesList.size(); j++) {
                    try {
//                        gsBranch.setLegal(String.valueOf(listBranch.get(j).getContractTel()));
//                        gsBranch.setEnterpriseName(listBranch.get(j).getComponyName());
                        gsBranch.setBelongOrg(branchesList.get(j).getBelongOrg());//分支机构管理局
                        gsBranch.setRegNo(branchesList.get(j).getRegNo());//分支机构信用代码
//                        gsBranch.setId(Long.valueOf(md5Branch.get(j).getId()));
                        gsBranchService.save(gsBranch);
                    } catch(Exception e){
                        log.error("", e);
                        log.debug("更新异常原因" + JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
            if(branchesList!=null){
                for (int j = 0; j < branchesList.size(); j++) {
                    if (md5Branch.size() == 0 && (branchesList.get(j).getBelongOrg() != null||branchesList.get(j).getRegNo() != null)) {
                        try {
                            log.debug("开始insert 表："+JSON.toJSON(uploadSecondData));
//                            gsBranch.setEnterpriseName(listBranch.get(j).getComponyName());
//                            gsBranch.setLegal(String.valueOf(listBranch.get(j).getContractTel()));
                            gsBranch.setCoMd5(gsBase.getMd5());
//                            gsBranch.setMd5(UUID.randomUUID().toString().replace("-",""));
                            gsBranch.setBelongOrg(branchesList.get(j).getBelongOrg());//分支机构管理局
                            gsBranch.setRegNo(branchesList.get(j).getRegNo());//分支机构信用代码
                            gsBranchService.save(gsBranch);
                        } catch (Exception e) {
                            log.error("", e);
                            log.debug("insert异常原因" + JSON.toJSON(uploadSecondData));
                            return false;
                        }
                    }
                }

            }
        /**股东信息表*/
        GsGudong gsGudong = new GsGudong();
        List<GsGudong> md5Gudong = gsGudongService.getByMd5(gsBase.getMd5());
        List<UploadSecondData.PartnersBean> listPartners = uploadSecondData.getPartners();
        for (int i = 0; i < md5Gudong.size(); i++) {
            //如果主表MD5和子表MD5有相同记录，则更新
            log.debug("开始更新gudong表："+JSON.toJSON(uploadSecondData));
                gsGudong.setCoMd5(gsBase.getMd5());
                gsGudongService.delete(gsGudong);
                for (int j = 0; j < listPartners.size(); j++) {
                    try {
//                        gsGudong.setShareholder((listShareholder.get(j).getSharholder()));
//                        gsGudong.setCapitalPercent(String.valueOf(listShareholder.get(j).getProportion()));
//                        gsGudong.setConfisCapital(String.valueOf(listShareholder.get(j).getMoney()));
//                        gsGudong.setCapitaltime(String.valueOf(listShar|eholder.get(j).getCreateTime()));
                        gsGudong.setStockType(String.valueOf(listPartners.get(j).getStockType()));//股东类型
//                        gsGudong.setCapitalType(String.valueOf(listPartners.get(j).getInvestType()));//认缴类型
//                        gsGudong.setId(Long.valueOf(md5Gudong.get(i).getId()));
                        gsGudongService.save(gsGudong);
                    } catch (Exception e) {
                        log.error("",e);
                        log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
        for (int j = 0; j < listPartners.size(); j++) {
            if (md5Gudong.size() == 0 && (listPartners.get(j).getInvestType() != null || listPartners.get(j).getStockType() != null)) {
                try {
                    log.debug("开始insert gudong表："+JSON.toJSON(uploadSecondData));
//                    gsGudong.setShareholder((listShareholder.get(j).getSharholder()));
//                    gsGudong.setCapitalPercent(String.valueOf(listShareholder.get(j).getProportion()));
//                    gsGudong.setConfisCapital(String.valueOf(listShareholder.get(j).getMoney()));
//                    gsGudong.setCapitaltime(String.valueOf(listShareholder.get(j).getCreateTime()));
//                    gsGudong.setCapitaltype(listPartners.get(j).getInvestType());//认缴类型
                    gsGudong.setStockType(listPartners.get(j).getStockType());//股东类型
                    gsGudong.setCoMd5(gsBase.getMd5());
//                    gsGudong.setMd5(UUID.randomUUID().toString().replace("-",""));

                    gsGudongService.save(gsGudong);
                } catch (Exception e) {
                    log.error("",e);
                    log.debug("insert异常原因"+ JSON.toJSON(uploadSecondData));
                    return false;
                }
            }
        }

        /**职员表*/
        GsMember gsMember = new GsMember();
        List<GsMember> gsMemberMd5 = gsMemberService.getByMd5(gsBase.getMd5());
        List<UploadSecondData.EmployeesBean> listEmployees= uploadSecondData.getEmployees();
        for (int i = 0; i < gsMemberMd5.size(); i++) {
            log.debug("开始更新member表："+JSON.toJSON(uploadSecondData));
                gsMember.setCoMd5(gsBase.getMd5());
                gsMemberService.delete(gsMember);
                for(int j=0;j<listEmployees.size();j++){
                try {
                    gsMember.setName(listEmployees.get(j).getName());
                    gsMember.setPosition(listEmployees.get(j).getJob());
//                    gsMember.setId(Long.valueOf(gsMemberMd5.get(i).getId()));
                    gsMemberService.save(gsMember);
                } catch (Exception e) {
                    log.error("",e);
                    log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                    return false;
                }
                }
            }
        if(listEmployees!=null){
        for(int j=0;j<listEmployees.size();j++) {
            if (gsMemberMd5.size() == 0 && (listEmployees.get(j).getJob() != null || listEmployees.get(j).getName()!=null)) {
                try {
                    log.debug("开始insert member表："+JSON.toJSON(uploadSecondData));
                    gsMember.setName(listEmployees.get(j).getName());
                    gsMember.setPosition(listEmployees.get(j).getJob());
//                    gsMember.setMd5(UUID.randomUUID().toString().replace("-",""));
                    gsMember.setCoMd5(gsBase.getMd5());
                    gsMemberService.save(gsMember);
                } catch (Exception e) {
                    log.error("", e);
                    log.debug("insert异常原因" + JSON.toJSON(uploadSecondData));
                    return false;
                }
            }
        }
        }
        /** 裁判本文*/
        GsGonggao gsGonggao= new GsGonggao();
        List<GsGonggao> gsGonggaoMd5 = gsGonggaoService.getByCoMd5(gsBase.getMd5());
        List<UploadSecondData.CpwsBean> listCpws = uploadSecondData.getCpws();
        for (int i = 0; i < gsGonggaoMd5.size(); i++) {
            log.debug("开始更新gonggao表："+JSON.toJSON(uploadSecondData));
                gsGonggao.setCoMd5(gsBase.getMd5());
                gsGonggaoService.delete(gsGonggao);
                for (int j = 0; j < listCpws.size(); j++) {
                    try {
                        gsGonggao.setCaseName(listCpws.get(j).getCaseName());//案件名
                        gsGonggao.setCaseNo(listCpws.get(j).getCaseNo());//案件号
                        gsGonggao.setCaseRole(listCpws.get(j).getCaseRole());//案件身份
//                        gsGonggao.setId(Long.valueOf(gsGonggaoMd5.get(i).getId()));
                        gsGonggaoService.save(gsGonggao);
                    } catch (Exception e) {
                        log.error("",e);
                        log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
    if(listCpws!=null){
        for (UploadSecondData.CpwsBean cpws : listCpws) {
            if (gsGonggaoMd5.size() == 0) {
                try {
                    log.debug("开始insert gonggao表："+JSON.toJSON(uploadSecondData));
                    gsGonggao.setCaseName(cpws.getCaseName());//案件名
                    gsGonggao.setCaseNo(cpws.getCaseNo());//案件号
                    gsGonggao.setCaseRole(cpws.getCaseRole());//案件身份
                    gsGonggao.setCoMd5(gsBase.getMd5());
//                    gsGonggao.setMd5(UUID.randomUUID().toString().replace("-", ""));
                    gsGonggaoService.save(gsGonggao);
                } catch (Exception e) {
                    log.error("",e);
                    log.debug("insert异常原因"+ JSON.toJSON(uploadSecondData));
                    return false;
                }
            }
        }
    }
        /**专利*/
        GsZhuanli gsZhuanli= new GsZhuanli();
        List<GsZhuanli> gsZhuanliMd5 = gsZhuanliService.getByMd5(gsBase.getMd5());
        List<UploadSecondData.PatentsBean> listPatents = uploadSecondData.getPatents();
        for (int i = 0; i < gsZhuanliMd5.size(); i++) {
            log.debug("开始更新zhuanli表："+JSON.toJSON(uploadSecondData));
                gsZhuanli.setCoMd5(gsBase.getMd5());
                gsZhuanliService.delete(gsZhuanli);
                for (int j = 0; j < listPatents.size(); j++) {
                    try {
                        gsZhuanli.setApplyPubNumber(listPatents.get(j).getApplicationNumber());//专利号
                        gsZhuanli.setDetail (listPatents.get(j).getKindCodeDesc());//详情
                        gsZhuanli.setPatentName(String.valueOf(listPatents.get(j).getTitle()));//专利名称
                        gsZhuanli.setApplicationTime(String.valueOf(listPatents.get(j).getApplicationDate()));//申请公布日
//                        gsZhuanli.setId(Long.valueOf(gsZhuanliMd5.get(i).getId()));
                        gsZhuanliService.save(gsZhuanli);
                    } catch (Exception e) {
                        log.error("",e);
                        log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
        if(listPatents!=null){
        for (int j = 0; j < listPatents.size(); j++) {
            if (gsZhuanliMd5.size() == 0 && (listPatents.get(j).getApplicationNumber() != null||listPatents.get(j).getTitle()!=null)) {
                try {
                    log.debug("开始insert zhuanli表："+JSON.toJSON(uploadSecondData));
                    gsZhuanli.setApplyPubNumber(listPatents.get(j).getApplicationNumber());//专利号
                    gsZhuanli.setDetail (listPatents.get(j).getKindCodeDesc());//详情
                    gsZhuanli.setPatentName(String.valueOf(listPatents.get(j).getTitle()));//专利名称
                    gsZhuanli.setApplicationTime(String.valueOf(listPatents.get(j).getApplicationDate()));//申请公布日
                    gsZhuanli.setCoMd5(gsBase.getMd5());
//                    gsZhuanli.setMd5(UUID.randomUUID().toString().replace("-", ""));
                    gsZhuanliService.save(gsZhuanli);
                } catch (Exception e) {
                    log.error("",e);
                    log.debug("insert异常原因"+ JSON.toJSON(uploadSecondData));
                    return false;
                }
            }
        }
        }
        /** 经营异常*/
        GsYichang gsYiChang= new GsYichang();
        List<GsYichang> gsYiChangMd5 = gsYichangService.getByMd5(gsBase.getMd5());
        List<UploadSecondData.JyycBean> listjyyc = uploadSecondData.getJyyc();
        for (int i = 0; i < gsYiChangMd5.size(); i++) {
            log.debug("开始更新yichang表："+JSON.toJSON(uploadSecondData));
                gsYiChang.setCoMd5(gsBase.getMd5());
                gsYichangService.delete(gsYiChang);
                for (int j = 0; j < listjyyc.size(); j++) {
                    try {
                        gsYiChang.setIncludeTime(String.valueOf(listjyyc.get(j).getAddDate()));//列入日期
                        gsYiChang.setIncludeReason(listjyyc.get(j).getAddReason());//列入原因
                        gsYiChang.setDecisionOrg(listjyyc.get(j).getDecisionOffice());//决定机关
                        gsYiChang.setRemoveTime(String.valueOf(listjyyc.get(j).getRemoveDate()));//移除日期
                        gsYiChang.setRemoveReason(listjyyc.get(j).getRomoveReason());//移除原因
                        gsYiChang.setRemoveOrg(listjyyc.get(j).getRemoveDecisionOffice());//移除机关
//                        gsYiChang.setId(Long.valueOf(gsYiChangMd5.get(i).getId()));
                        gsYichangService.save(gsYiChang);
                    } catch (Exception e) {
                        log.error("",e);
                        log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
        if(listjyyc!=null) {
            for (int j = 0; j < listjyyc.size(); j++) {
                if (gsYiChangMd5.size() == 0 && (listjyyc.get(j).getAddReason() != null || listjyyc.get(j).getRomoveReason() != null)) {
                    try {
                        log.debug("开始insert yichang表："+JSON.toJSON(uploadSecondData));
                        gsYiChang.setIncludeTime(String.valueOf(listjyyc.get(j).getAddDate()));//列入日期
                        gsYiChang.setIncludeReason(listjyyc.get(j).getAddReason());//列入原因
                        gsYiChang.setDecisionOrg(listjyyc.get(j).getDecisionOffice());//决定机关
                        gsYiChang.setRemoveTime(String.valueOf(listjyyc.get(j).getRemoveDate()));//移除日期
                        gsYiChang.setRemoveReason(listjyyc.get(j).getRomoveReason());//移除原因
                        gsYiChang.setRemoveOrg(listjyyc.get(j).getRemoveDecisionOffice());//移除机关
                        gsYiChang.setCoMd5(gsBase.getMd5());
//                    gsYiChang.setMd5(UUID.randomUUID().toString().replace("-", ""));
                        gsYichangService.save(gsYiChang);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("insert异常原因" + JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
        }
        /** 软件著作*/
        GsZhuzuoquan gsZhuzuoquan= new GsZhuzuoquan();
        List<GsZhuzuoquan> gsZhuzuoquanMd5 = gsZhuzuoquanService.getByMd5(gsBase.getMd5());
        List<UploadSecondData.SoftWaresBean> listSoftWares = uploadSecondData.getSoftWares();
        for (int i = 0; i < gsZhuzuoquanMd5.size(); i++) {
            log.debug("开始更新zhuzuoquan表："+JSON.toJSON(uploadSecondData));
                gsZhuzuoquan.setCoMd5(gsBase.getMd5());
                gsZhuzuoquanService.delete(gsZhuzuoquan);
                for (int j = 0; j < listSoftWares.size(); j+=gsZhuzuoquanMd5.size()) {
                    try {
                        gsZhuzuoquan.setVersionNumber(listSoftWares.get(j).getVersionNo());//版本号
                        gsZhuzuoquan.setRegNumber(listSoftWares.get(j).getRegisterNo());//注册号
                        gsZhuzuoquan.setApprovalTime(String.valueOf(listSoftWares.get(j).getRegisterAperDate())); //日期
                        gsZhuzuoquan.setFullName(listSoftWares.get(j).getName());//名称
//                        gsZhuzuoquan.setId(Long.valueOf(gsZhuzuoquanMd5.get(i).getId()));
                        gsZhuzuoquanService.save(gsZhuzuoquan);
                    } catch (Exception e) {
                        log.error("",e);
                        log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
        if(listSoftWares!=null){
        for (int j = 0; j < listSoftWares.size(); j++) {
            if (gsZhuzuoquanMd5.size() == 0 &&(listSoftWares.get(j).getName() != null||listSoftWares.get(j).getRegisterNo()!=null)) {
                try {
                    log.debug("开始insert zhuzuoquan表："+JSON.toJSON(uploadSecondData));
                    gsZhuzuoquan.setVersionNumber(listSoftWares.get(j).getVersionNo());//版本号
                    gsZhuzuoquan.setRegNumber(listSoftWares.get(j).getRegisterNo());//注册号
                    gsZhuzuoquan.setApprovalTime(String.valueOf(listSoftWares.get(j).getRegisterAperDate())); //日期
                    gsZhuzuoquan.setFullName(listSoftWares.get(j).getName());//名称
                    gsZhuzuoquan.setCoMd5(gsBase.getMd5());
//                    gsZhuzuoquan.setMd5(UUID.randomUUID().toString().replace("-", ""));
                    gsZhuzuoquanService.save(gsZhuzuoquan);
                } catch (Exception e) {
                    log.error("",e);
                    log.debug("insert异常原因"+ JSON.toJSON(uploadSecondData));
                    return false;
                }
            }
        }
        }

        /**商标*/
        GsShangbiao gsShangbiao= new GsShangbiao();
        List<GsShangbiao> gsShangbiaoMd5 = gsShangbiaoService.getByMd5(gsBase.getMd5());
        List<UploadSecondData.TradeMarkersBean> listTradeMarkers = uploadSecondData.getTradeMarkers();
        for (int i = 0; i < gsShangbiaoMd5.size(); i++) {
            log.debug("开始更新shangbiao表："+JSON.toJSON(uploadSecondData));
                gsShangbiao.setCoMd5(gsBase.getMd5());
                gsShangbiaoService.delete(gsShangbiao);
                for (int j = 0; j < listTradeMarkers.size(); j++) {
                    try {
                        gsShangbiao.setApplyTime(String.valueOf(listTradeMarkers.get(j).getAppDate()));//申请日期
                        gsShangbiao.setTrademarkName(listTradeMarkers.get(j).getName());//商标名称
                        gsShangbiao.setRegNumber(listTradeMarkers.get(j).getRegNo());//注册号
                        gsShangbiao.setState(uploadSecondData.getStatus());//状态
//                        gsShangbiao.setType();//类别
//                        gsShangbiao.setId(Long.valueOf(gsShangbiaoMd5.get(i).getId()));
                        gsShangbiaoService.save(gsShangbiao);
                    } catch (Exception e) {
                        log.error("",e);
                        log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
        if(listTradeMarkers!=null){
        for (int j = 0; j < listTradeMarkers.size(); j++) {
            if (gsShangbiaoMd5.size() == 0 && (listTradeMarkers.get(j).getName()!= null||listTradeMarkers.get(j).getRegNo()!=null)) {
                try {
                    log.debug("开始insert shangbiao表："+JSON.toJSON(uploadSecondData));
                    gsShangbiao.setApplyTime(String.valueOf(listTradeMarkers.get(j).getAppDate()));//申请日期
                    gsShangbiao.setTrademarkName(listTradeMarkers.get(j).getName());//商标名称
                    gsShangbiao.setRegNumber(listTradeMarkers.get(j).getRegNo());//注册号
                    gsShangbiao.setState(uploadSecondData.getStatus());//状态
                    gsShangbiao.setCoMd5(gsBase.getMd5());
//                    gsShangbiao.setMd5(UUID.randomUUID().toString().replace("-", ""));
                    gsShangbiaoService.save(gsShangbiao);
                } catch (Exception e) {
                    log.error("",e);
                    log.debug("insert异常原因"+ JSON.toJSON(uploadSecondData));
                    return false;
                }
            }
        }
        }
        /**对外投资*/
        GsOuttouzi gsOuttouzi= new GsOuttouzi();
        List<GsOuttouzi> gsOuttouzitMd5 = gsOuttouziService.getByMd5(gsBase.getMd5());
        List<UploadSecondData.DwtzBean> listDwtz = uploadSecondData.getDwtz();
        for (int i = 0; i < gsOuttouzitMd5.size(); i++) {
            log.debug("开始更新outtouzi表："+JSON.toJSON(uploadSecondData));
                gsOuttouzi.setCoMd5(gsBase.getMd5());
                gsOuttouziService.delete(gsOuttouzi);
                for (int j = 0; j < listDwtz.size(); j++) {
                    try {
                        gsOuttouzi.setEnterpriseName(listDwtz.get(j).getName());//被投资企业名称
                        gsOuttouzi.setInvestmentRatio (listDwtz.get(j).getFundedRatio());//投资占比
                        gsOuttouzi.setState((listDwtz.get(j).getStatus()));//状态
                        gsOuttouzi.setCreditCode(listDwtz.get(j).getCreditCode());//对外投资企业信用代码
//                        gsOuttouzi.setId(Long.valueOf(gsOuttouzitMd5.get(i).getId()));
                        gsOuttouziService.save(gsOuttouzi);
                    } catch (Exception e) {
                        log.error("",e);
                        log.debug("更新异常原因"+ JSON.toJSON(uploadSecondData));
                        return false;
                    }
                }
            }
        if(listDwtz!=null){
        for (int j = 0; j < listDwtz.size(); j++) {
            if (gsOuttouzitMd5.size() == 0 &&(listDwtz.get(j).getFundedRatio() != null||listDwtz.get(j).getName()!=null)) {
                try {
                    log.debug("开始insert outtouzi表："+JSON.toJSON(uploadSecondData));
                    gsOuttouzi.setEnterpriseName(listDwtz.get(j).getName());//被投资企业名称
                    gsOuttouzi.setInvestmentRatio (listDwtz.get(j).getFundedRatio());//投资占比
                    gsOuttouzi.setState((listDwtz.get(j).getStatus()));//状态
                    gsOuttouzi.setCreditCode(listDwtz.get(j).getCreditCode());//对外投资企业信用代码
                    gsOuttouzi.setCoMd5(gsBase.getMd5());
//                    gsOuttouzi.setMd5(UUID.randomUUID().toString().replace("-", ""));
                    gsOuttouziService.save(gsOuttouzi);
                } catch (Exception e) {
                    log.error("",e);
                    log.debug("insert异常原因"+ JSON.toJSON(uploadSecondData));
                    return false;
                }
            }
        }
        }
        /**日志*/
            GsReportLog gsReportLog = new GsReportLog();
            gsReportLog.setKeyNo(gsBase.getMd5());//keyNo
            gsReportLog.setName(uploadSecondData.getComponyName());//公司名称
            gsReportLog.setDataRecord(JSON.toJSON(uploadSecondData).toString());//请求数据
            Date date = new Date();
            gsReportLog.setUpdateTime(date.toLocaleString());
            gsReportLogService.save(gsReportLog);

        return true;
    }
}
